package com.example.mokmap2019;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private MapView mMapView ;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    UiSettings mUiSettings;

    private CheckBox rbtn3;
    private CheckBox rbtn4;


    private PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
//        地图Logo:
//        通过以下方法，使用枚举类型控制显示的位置，共支持6个显示位置(左下，中下，右下，左上，中上，右上)。
//        mMapView.setLogoPosition(LogoPosition.logoPostionleftBottom);
        mMapView.setLogoPosition(LogoPosition.logoPostionleftTop);//左上角

        //实例化UiSettings类对象
        mUiSettings = mBaiduMap.getUiSettings();
        //通过设置enable为true或false 选择是否显示指南针
        mUiSettings.setCompassEnabled(true);
        //通过设置enable为true或false 选择是否显示比例尺
        mMapView.showScaleControl(true);
        //通过设置enable为true或false 选择是否显示缩放按钮
        mMapView.showZoomControls(true);
        //通过设置enable为true或false 选择是否启用地图平移
        mUiSettings.setScrollGesturesEnabled(true);
        //通过设置enable为true或false 选择是否启用地图缩放手势
        mUiSettings.setZoomGesturesEnabled(true);
        //通过设置enable为true或false 选择是否启用地图俯视功能
        mUiSettings.setOverlookingGesturesEnabled(true);
        //通过设置enable为true或false 选择是否启用地图旋转功能
        mUiSettings.setRotateGesturesEnabled(true);
        //获取地图控件引用
        BaiduMap.OnMapLongClickListener listener1 = new BaiduMap.OnMapLongClickListener() {
            /**
             * 地图长按事件监听回调函数
             *
             * @param point 长按的地理坐标
             */
            @Override
            public void onMapLongClick(LatLng point) {

//构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.loc);
//构建MarkerOption，用于在地图上添加Marker
                OverlayOptions option = new MarkerOptions()
                        .position(point)
                        .icon(bitmap);
//在地图上添加Marker，并显示
                mBaiduMap.addOverlay(option);
            }
        };
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            //marker被点击时回调的方法
            //若响应点击事件，返回true，否则返回false
            //默认返回false
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this, "该Marker被点击", Toast.LENGTH_LONG).show();
                return true;
            }
        });
//设置地图长按事件监听
        BaiduMap.OnMapDoubleClickListener listener2 = new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng point) {
////构建折线点坐标
//                LatLng p1 = new LatLng(point.latitude, point.longitude);
//                LatLng p2 = new LatLng(point.latitude-0.004, point.longitude+0.004);
//                LatLng p3 = new LatLng(point.latitude, point.longitude+0.008);
//                List<LatLng> points = new ArrayList<LatLng>();
//                points.add(p1);
//                points.add(p2);
//                points.add(p3);
////设置折线的属性
//                OverlayOptions mOverlayOptions = new PolylineOptions()
//                        .width(10)
//                        .color(0xAAFF0000)
//                        .points(points);
////在地图上绘制折线
////mPloyline 折线对象
//                Overlay mOverlayOptions=  mBaiduMap.addOverlay(mOverlayOptions);
////在地图上绘制圆：
/*                //圆心位置
                LatLng center = new LatLng(point.latitude, point.longitude);

//构造CircleOptions对象
                CircleOptions mCircleOptions = new CircleOptions().center(center)
                        .radius(1400)
                        .fillColor(0xAA0000FF) //填充颜色
                        .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

//在地图上显示圆
                Overlay mCircle = mBaiduMap.addOverlay(mCircleOptions);*/
//文字覆盖物位置坐标
                LatLng llText = new LatLng(point.latitude, point.longitude);

////构建TextOptions对象
//                OverlayOptions mTextOptions = new TextOptions()
//                        .text("百度地图SDK") //文字内容
//                        .bgColor(0xAAFFFF00) //背景色
//                        .fontSize(24) //字号
//                        .fontColor(0xFFFF00FF) //文字颜色
//                        .rotate(-30) //旋转角度
//                        .position(llText);
//
////在地图上显示文字覆盖物
//                Overlay mText = mBaiduMap.addOverlay(mTextOptions);
                //用来构造InfoWindow的Button
                Button button = new Button(getApplicationContext());
                button.setBackgroundResource(R.drawable.info);
                button.setText("InfoWindow");

//构造InfoWindow
//point 描述的位置点
//-100 InfoWindow相对于point在y轴的偏移量
                InfoWindow mInfoWindow = new InfoWindow(button, point, -100);

//使InfoWindow生效
                mBaiduMap.showInfoWindow(mInfoWindow);
            }
        };

//设置地图双击事件监听
        mBaiduMap.setOnMapDoubleClickListener(listener2);
        mBaiduMap.setOnMapLongClickListener(listener1);
        initView(mBaiduMap);
        //通过LocationClient定位到当前位置：
        findViewById(R.id.lactionToGDCP).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationToGDCP(mBaiduMap);
            }
        });

    }

    private void locationToGDCP(BaiduMap mBaiduMap) {
        mBaiduMap.setMyLocationEnabled(true);
        mLocationClient = new LocationClient(this);

//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);

//设置locationClientOption
        mLocationClient.setLocOption(option);

//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();


    }

    private void initView(final BaiduMap mBaiduMap) {
        mBaiduMap.setMyLocationEnabled(true);
        findViewById(R.id.Rbtn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 //普通地图 ,mBaiduMap是地图控制器对象
                mBaiduMap.clear();
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
            }
        });
        findViewById(R.id.Rbtn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 //卫星地图
                mBaiduMap.clear();
                mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
            }
        });
        rbtn3 =  findViewById(R.id.Rbtn3);
        rbtn3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //开启交通图
                    mBaiduMap.setTrafficEnabled(true);
                }
                if(!isChecked) {
                    mBaiduMap.setTrafficEnabled(false);
                }
            }
        });
        rbtn4 =  findViewById(R.id.Rbtn4);
        rbtn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //开启热力图
                    mBaiduMap.setBaiduHeatMapEnabled(true);
                }
                if(!isChecked) {
                    mBaiduMap.setBaiduHeatMapEnabled(false);
                }
            }
        });


        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });
        findViewById(R.id.poibtn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 *  PoiCiySearchOption 设置检索属性
                 *  city 检索城市
                 *  keyword 检索内容关键字
                 *  pageNum 分页页码
                 */
                poiSearch.searchInCity(new PoiCitySearchOption()
                        .city("北京") //必填
                        .keyword("美食") //必填
                        .pageNum(10));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
//    我们通过继承抽象类BDAbstractListener并重写其onReceieveLocation方法来获取定位数据，并将其传给MapView
    class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
        }
    }
}
